<template>
  <div class="relnew">
    <div style="margin-bottom: 10vh;">
      <van-collapse v-model="activeNames">
        <van-collapse-item v-for="item in jsondata.head" :key="item.grouptitle" :name="item.grouptitle" :title="item.grouptitle">
          <van-cell-group style="border:0px;">
            <div style="border-bottom:1px solid #ebedf0" v-for="itemdata in item.groupvalue" :key="itemdata.ID">
              <div v-if="itemdata.sUIType == 'datetime'">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" v-model="itemdata.value" required disabled clearable :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field :id="itemdata.sField" v-model="itemdata.value" required clearable :label="itemdata.sTitle" @click="openDatePicker(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" v-model="itemdata.value" disabled clearable :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field :id="itemdata.sField" v-model="itemdata.value" clearable :label="itemdata.sTitle" @click="openDatePicker(itemdata)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'code'">
                <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
              </div>
              <div v-else-if="itemdata.sUIType == 'enum' && itemdata.sUITypeExp == 'normal'">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnum(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnum(itemdata)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'enum' && itemdata.sUITypeExp == 'multi'">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnumMulti(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnumMulti(itemdata)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'ref' && itemdata.sUITypeExp == 'normal'">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRef(itemdata, 'headdata', 0)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRef(itemdata, 'headdata', 0)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'ref' && itemdata.sUITypeExp == 'multi'">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'headdata', 0)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'headdata', 0)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'bool'">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_bool'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_bool'" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_bool'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_bool'" v-model="itemdata.value" :label="itemdata.sTitle" @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'string' && itemdata.sFormat == 'star'">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-cell :title="itemdata.sTitle">
                    <template #default>
                      <van-rate v-model="itemdata.value" :size="25" color="#ffd21e" void-icon="star" void-color="#eee" />
                    </template>
                  </van-cell>
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-cell :title="itemdata.sTitle">
                    <template #default>
                      <van-rate v-model="itemdata.value" :size="25" color="#ffd21e" void-icon="star" void-color="#eee" />
                    </template>
                  </van-cell>
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-cell :title="itemdata.sTitle">
                    <template #default>
                      <van-rate v-model="itemdata.value" :size="25" color="#ffd21e" void-icon="star" void-color="#eee" />
                    </template>
                  </van-cell>
                </template>
                <template v-else>
                  <van-cell :title="itemdata.sTitle">
                    <template #default>
                      <van-rate v-model="itemdata.value" :size="25" color="#ffd21e" void-icon="star" void-color="#eee" />
                    </template>
                  </van-cell>
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'file'">
                <div style="color:#323233;font-size:14px;line-height:24px;padding:5px;">{{ itemdata.sTitle }}</div>
                <input type="hidden" v-model="itemdata.valuehidden" />
                <input type="hidden" v-model="itemdata.value" />
                <van-uploader v-model="itemdata.valuelist" :after-read="onFileHead" @delete="delFileHead" accept="*" multiple :name="itemdata.sField">
                </van-uploader>
              </div>
              <div v-else-if="itemdata.sUIType == 'picture'">
                <div style="color:#323233;font-size:14px;line-height:24px;padding:5px;">{{ itemdata.sTitle }}</div>
                <input type="hidden" v-model="itemdata.valuehidden" />
                <input type="hidden" v-model="itemdata.value" />
                <van-uploader v-model="itemdata.valuelist" :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField">
                </van-uploader>
              </div>
              <div v-else-if="itemdata.sUIType == 'string' && (itemdata.sUITypeExp == 'long' || itemdata.sUITypeExp == 'big')">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field type="textarea" rows="1" autosize :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field type="textarea" rows="1" autosize :id="itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field type="textarea" rows="1" autosize :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field type="textarea" rows="1" autosize :id="itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'string' && itemdata.sUITypeExp == 'normal' && itemdata.sFormat == 'yearmonth;'">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field :id="itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field :id="itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'string' && itemdata.sUITypeExp == 'normal' && itemdata.sFormat.indexOf('refer:') >= 0">
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field :id="itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openRefer(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field :id="itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" @click="openRefer(itemdata)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else>
                <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field :id="itemdata.sField" required clearable v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field :id="itemdata.sField" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
              </div>
            </div>
          </van-cell-group>
        </van-collapse-item>
      </van-collapse>

      <div v-for="(item, indexName) in jsondata.body" :key="indexName">
        <van-collapse v-model="activeBodyNams">
          <van-collapse-item v-for="(item, index) in jsondata.bodydata[indexName]" :key="index" :name="indexName + index" :title="indexName + (index + 1)">
            <van-cell-group style="border:0px;">
              <div style="border-bottom:1px solid #ebedf0" v-for="itemdata in item" :key="itemdata.ID">
                <div v-if="itemdata.sUIType == 'datetime'">
                  <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                    <van-field :id="index + '_' + itemdata.sField" v-model="itemdata.value" required disabled clearable :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lMustInput === '1'">
                    <van-field :id="index + '_' + itemdata.sField" v-model="itemdata.value" required clearable :label="itemdata.sTitle" @click="openDatePicker(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lCanInput === '0'">
                    <van-field :id="index + '_' + itemdata.sField" v-model="itemdata.value" disabled clearable :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else>
                    <van-field :id="index + '_' + itemdata.sField" v-model="itemdata.value" required clearable :label="itemdata.sTitle" @click="openDatePicker(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                </div>
                <div v-else-if="itemdata.sUIType == 'code'">
                  <van-field :id="index + '_' + itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </div>
                <div v-else-if="itemdata.sUIType == 'enum' && itemdata.sUITypeExp == 'normal'">
                  <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_enum'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lMustInput === '1'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_enum'" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnum(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_enum'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else>
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_enum'" v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnum(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                </div>
                <div v-else-if="itemdata.sUIType == 'enum' && itemdata.sUITypeExp == 'multi'">
                  <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_enum'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lMustInput === '1'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_enum'" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnumMulti(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_enum'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else>
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_enum'" v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnumMulti(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                </div>
                <div v-else-if="itemdata.sUIType == 'ref' && itemdata.sUITypeExp == 'normal'">
                  <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_ref'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lMustInput === '1'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRef(itemdata, 'bodydata', index)" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_ref'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else>
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRef(itemdata, 'bodydata', index)" :error-message="itemdata.errormsg" />
                  </template>
                </div>
                <div v-else-if="itemdata.sUIType == 'ref' && itemdata.sUITypeExp == 'multi'">
                  <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_ref'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lMustInput === '1'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'bodydata', index)" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_ref'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else>
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'bodydata', index)" :error-message="itemdata.errormsg" />
                  </template>
                </div>
                <div v-else-if="itemdata.sUIType == 'bool'">
                  <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_bool'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lMustInput === '1'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_bool'" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lCanInput === '0'">
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_bool'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else>
                    <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
                    <van-field :id="index + '_' + itemdata.sField + '_bool'" v-model="itemdata.value" :label="itemdata.sTitle" @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                </div>
                <div v-else-if="itemdata.sUIType == 'string' && (itemdata.sUITypeExp == 'long' || itemdata.sUITypeExp == 'big')">
                  <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                    <van-field type="textarea" rows="1" autosize :id="index + '_' + itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lMustInput === '1'">
                    <van-field type="textarea" rows="1" autosize :id="index + '_' + itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lCanInput === '0'">
                    <van-field type="textarea" rows="1" autosize :id="index + '_' + itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else>
                    <van-field type="textarea" rows="1" autosize :id="index + '_' + itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                </div>
                <div v-else-if="itemdata.sUIType == 'string' && itemdata.sUITypeExp == 'normal' && itemdata.sFormat == 'yearmonth;'">
                  <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                    <van-field :id="index + '_' + itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lMustInput === '1'">
                    <van-field :id="index + '_' + itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lCanInput === '0'">
                    <van-field :id="index + '_' + itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else>
                    <van-field :id="index + '_' + itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                </div>
                <div v-else>
                  <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                    <van-field :id="index + '_' + itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lMustInput === '1'">
                    <van-field :id="index + '_' + itemdata.sField" required clearable v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else-if="itemdata.lCanInput === '0'">
                    <van-field :id="index + '_' + itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                  <template v-else>
                    <van-field :id="index + '_' + itemdata.sField" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                  </template>
                </div>
              </div>
              <van-button type="danger" size="mini" plain round style="" @click="delRow(indexName, index)">删除</van-button>
            </van-cell-group>
          </van-collapse-item>
        </van-collapse>
        <van-button v-show="jsondata.hasdetail === '1'" type="info" round style="margin-top: 2vh;margin-left: 2vw;" @click="addRow(indexName)">增行</van-button>
      </div>

    </div>
    <van-popup v-model="datepickershow" round position="bottom" :style="{ height: '40%' }">
      <van-datetime-picker v-model="currentDate" type="date" @confirm="selectedDate" />
    </van-popup>
    <van-popup v-model="datetimeshow" round position="bottom">
      <van-datetime-picker v-model="currentDate" type="datetime" @confirm="selectedDateTime" />
    </van-popup>
    <van-popup v-model="yearmonthshow" round position="bottom">
      <van-datetime-picker v-model="currentYearMonth" type="year-month" @confirm="selectedYearMonth" />
    </van-popup>
    <van-action-sheet v-model="enumshow" :actions="actions" @select="selectedEnum" />
    <van-popup v-model="enumMultishow" round position="bottom">
      <van-checkbox-group v-model="selecteddata" ref="checkboxGroup">
        <van-cell v-for="(item, index) in enumMulti" :key="item.id" :title="item.name" @click="toggle(index)">
          <van-checkbox :name="item" ref="checkboxes" slot="right-icon" />
        </van-cell>
      </van-checkbox-group>
      <div style="text-align:right;padding-right:10vw;padding-top:2vh;padding-bottom:2vh;">
        <van-button type="info" size="small" @click="selectedEnumMulti">确认</van-button>
      </div>
    </van-popup>
    <van-tabbar v-model="active" safe-area-inset-bottom>
      <van-tabbar-item @click="save">保存</van-tabbar-item>
      <van-tabbar-item v-if="isflow" @click="savesubmit">保存并提交</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import {
  Search,
  List,
  Cell,
  PullRefresh,
  Toast,
  Tabbar,
  TabbarItem,
  Popup,
  DatetimePicker,
  ActionSheet,
  Tab,
  Tabs,
  Collapse,
  CollapseItem,
  CheckboxGroup,
  Checkbox,
  Uploader,
  Rate
} from 'vant'
import qs from 'qs'
import { fieldLinkMixins } from '@/mixins/fieldLink.js'
import { fieldMapMixins } from '@/mixins/fieldmap.js'
import { eventCommonMixins } from '@/mixins/eventCommon.js'

Vue.use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(DatetimePicker).use(ActionSheet).use(Tab).use(Tabs).use(Collapse).use(CollapseItem).use(Checkbox).use(CheckboxGroup).use(Uploader).use(Rate)
export default {
  name: 'relnew',
  mixins: [fieldLinkMixins, fieldMapMixins, eventCommonMixins],
  data () {
    return {
      relfield: this.$route.query.relfield,
      relvalue: this.$route.query.relvalue,
      relobject: this.$route.query.relobject,
      idobject: this.$route.query.idobject,
      jsondata: {},
      index: 0,
      active: 0,
      activeNames: [''],
      activeBodyNams: [''],
      datepickershow: false,
      datetimeshow: false,
      currentDate: new Date(),
      yearmonthshow: false,
      currentYearMonth: new Date(),
      enumshow: false,
      actions: [],
      clickmodel: {},
      isLoad: false,
      conditionValHidden: '',
      clickfrom: '',
      clickindex: 0,
      selecteddata: [],
      enumMultishow: false,
      enumMulti: [],
      isDisable: false,
      isflow: false
    }
  },
  mounted: function () {
    //console.log('rel new created')
    if (!this.isLoad) {
      let data = { [this.relobject + '_' + this.relfield]: this.relvalue }
      this.axios({
        method: 'post',
        url: '/api/' + this.relobject + '.new.do',
        data: qs.stringify(data)
      }).then(res => {
        //console.log(res)
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          }
        } else {
          this.jsondata = res.data.jsondata
          this.isflow = res.data.isflow
          for (let i = 0; i < res.data.jsondata.head.length; i++) {
            this.activeNames.push(res.data.jsondata.head[i].grouptitle)
          }

          // 字段联动变量
          this.orginData = JSON.parse(JSON.stringify(this.jsondata))
          this.controlLinkArr = this.getFieldLink(this.jsondata)
          if (this.jsondata.hasdetail != '0') {
            this.controlLinkArrDetail = this.getFieldLinkDetail(this.jsondata)
          }
          // 字段联动初始化
          this.firstHandleFiledLink(this.jsondata)

          //console.log(this.jsondata)
          this.isLoad = true
        }
      }).catch(res => {
        this.reserror(res)
      })
    }
  },
  methods: {
    addRow (indexName) {
      let bodycopy = JSON.parse(JSON.stringify(this.jsondata.body[indexName]))
      this.jsondata.bodydata[indexName].push(bodycopy)
    },
    delRow (indexName, index) {
      if (this.jsondata.bodydata[indexName].length === 1) {
        Toast('至少保留一条表体行，不能删除！')
        return
      }
      this.jsondata.bodydata[indexName].splice(index, 1)
    },
    save () {
      if (!this.isDisable) {
        this.isDisable = true
        let data = {}
        let cansave = 1
        data[this.relobject + '_id'] = this.jsondata[this.relobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        data[this.relobject + '_' + this.relfield] = this.relvalue

        if (this.jsondata.bodydata) {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }

        if (cansave === 0) {
          this.isDisable = false
          Toast('请检查表头表体必填项')
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.relobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          if (res.data.flag === '0') {
            Toast('保存成功')
            this.$router.push({ name: 'card', query: { itemid: this.relvalue, idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          //console.log(res)
        })
      }
    },
    savesubmit () {
      if (!this.isDisable) {
        this.isDisable = true
        let data = {}
        let cansave = 1
        data[this.relobject + '_id'] = this.jsondata[this.relobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        data[this.relobject + '_' + this.relfield] = this.relvalue
        if (this.jsondata.bodydata) {
          for (let i = 0; i < this.jsondata.bodydata.length; i++) {
            let item = this.jsondata.bodydata[i]
            for (let itemindex in item) {
              let itemdata = item[itemindex]
              if (itemdata.sUIType === 'ref') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
              } else if (itemdata.sUIType === 'enum') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
              } else {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.relobject + '.newsavesubmit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          if (res.data.flag === '0') {
            Toast('保存成功')
            this.$router.push({ name: 'card', query: { itemid: this.relvalue, idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          //console.log(res)
        })
      }
    },
    checkData (datamodel) {
      if (datamodel.value === '') {
        datamodel.errormsg = '不能为空'
      } else {
        datamodel.errormsg = ''
      }
    },
    openDatePicker (datamodel) {
      if (datamodel.value != null && datamodel.value !== undefined && datamodel.value !== '') {
        if (datamodel.sUITypeExp !== 'long') {
          datamodel.value = datamodel.value.substring(0, 10)
        }
        let newDate = new Date(datamodel.value.replace(/-/g, '/'))
        this.currentDate = new Date(newDate)
      }
      this.clickmodel = datamodel
      if (datamodel.sUITypeExp === 'long') {
        this.datetimeshow = true
      } else {
        this.datepickershow = true
      }
    },
    selectedDate () {
      //console.log(this.currentDate)
      let year = this.currentDate.getFullYear() // 获取完整的年份(4位,1970-????)
      let month = this.currentDate.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
      let day = this.currentDate.getDate() // 获取当前日(1-31)
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      let dateString = year + '-' + month + '-' + day
      this.clickmodel.value = dateString
      this.clickmodel.errormsg = ''
      this.datepickershow = false
    },
    selectedDateTime () {
      //console.log(this.currentDate)
      let year = this.currentDate.getFullYear() // 获取完整的年份(4位,1970-????)
      let month = this.currentDate.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
      let day = this.currentDate.getDate() // 获取当前日(1-31)
      let hour = this.currentDate.getHours()
      let minute = this.currentDate.getMinutes()
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      if (hour < 10) {
        hour = '0' + hour
      }
      if (minute < 10) {
        minute = '0' + minute
      }
      let dateString = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':00'
      this.clickmodel.value = dateString
      this.clickmodel.errormsg = ''
      this.datetimeshow = false
    },
    openYearMonth (datamodel) {
      if (datamodel.value != null && datamodel.value !== undefined && datamodel.value !== '') {
        datamodel.value = datamodel.value.substring(0, 7)
        this.currentYearMonth = new Date(datamodel.value)
      }
      this.clickmodel = datamodel
      this.yearmonthshow = true
    },
    selectedYearMonth () {
      //console.log(this.currentYearMonth)
      let year = this.currentYearMonth.getFullYear()
      let month = this.currentYearMonth.getMonth() + 1
      if (month < 10) {
        month = '0' + month
      }
      let datestring = year + '-' + month
      this.clickmodel.value = datestring
      this.clickmodel.errormsg = ''
      this.yearmonthshow = false
    },
    openEnum (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      let enumid = datamodel.allenumid
      let arrEnumid = enumid.split(';')
      let enumval = datamodel.allenumval
      let arrEnumval = enumval.split(';')
      for (let i = 0; i < arrEnumid.length; i++) {
        this.actions.push({ id: arrEnumid[i], name: arrEnumval[i] })
      }
      this.enumshow = true
    },
    selectedEnum (item) {
      this.clickmodel.valuehidden = item.id
      this.clickmodel.value = item.name
      this.clickmodel.errormsg = ''
      this.enumshow = false
    },
    openEnumMulti (datamodel) {
      this.enumMulti = []
      this.selecteddata = []
      this.clickmodel = datamodel
      let enumid = datamodel.allenumid
      let arrEnumid = enumid.split(';')
      let enumval = datamodel.allenumval
      let arrEnumval = enumval.split(';')
      for (let i = 0; i < arrEnumid.length; i++) {
        if (arrEnumid[i] === '') {
          continue
        }
        this.enumMulti.push({ id: arrEnumid[i], name: arrEnumval[i] })
      }
      this.enumMultishow = true
    },
    toggle (index) {
      this.$refs.checkboxes[index].toggle()
    },
    selectedEnumMulti () {
      let enumid = ''
      let enumval = ''
      this.clickmodel.valuehidden = ''
      this.clickmodel.value = ''
      for (let i = 0; i < this.selecteddata.length; i++) {
        enumid += this.selecteddata[i].id + ';'
        enumval += this.selecteddata[i].name + ';'
      }
      this.clickmodel.valuehidden = enumid
      this.clickmodel.value = enumval
      this.clickmodel.errormsg = ''
      this.enumMultishow = false
    },
    openRefer (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      let referVal = datamodel.sFormat.substring(6, datamodel.sFormat.length - 1)
      let arrRefer = referVal.split(',')
      for (let i = 0; i < arrRefer.length; i++) {
        this.actions.push({ id: arrRefer[i], name: arrRefer[i] })
      }
      this.enumshow = true
    },
    openBool (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      this.actions.push({ id: '1', name: '是' })
      this.actions.push({ id: '0', name: '否' })
      this.enumshow = true
    },
    openRef (datamodel, datafrom, index) {
      this.clickmodel = datamodel
      this.clickfrom = datafrom
      this.clickindex = index
      this.conditionValHidden = ''
      let inview = datamodel.inview
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[index]) {
                let item = this.jsondata.bodydata[index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            this.conditionValHidden += inviewdatas[0] + ':in:;'
          }
        }
      }
      let componentName = 'ref'
      if (datamodel.idJoinObject === 'employee' || datamodel.idJoinObject === 'dep') {
        componentName = 'orgRef'
      }
      this.$router.push({
        name: componentName,
        params: {
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden
        }
      })
    },
    openRefMulti (datamodel, datafrom, index) {
      this.clickmodel = datamodel
      this.clickfrom = datafrom
      this.clickindex = index
      this.conditionValHidden = ''
      let inview = datamodel.inview
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[index]) {
                let item = this.jsondata.bodydata[index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            this.conditionValHidden += inviewdatas[0] + ':in:;'
          }
        }
      }
      // 每个逻辑需要单独写
      if (datamodel.idObject === 'sorders' && datamodel.jsfunc === 'getAccountForSorder()') {
        let agent = ''
        let consumer = ''
        let type = ''
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex]
            if (item.sField === 'agent') {
              agent = item.valuehidden
            } else if (item.sField === 'consumer') {
              consumer = item.valuehidden
            } else if (item.sField === 'type') {
              type = item.valuehidden
            }
          }
        }
        if (consumer === '') {
          Toast('请先选择客户')
          return
        }
        if (type === '297ef43353a100de0153a1fdcaa60611') {
          if (agent !== '') {
            Toast('自营模式不能输入代理商')
            return
          }
        } else {
          if (agent === '') {
            Toast('请先选择代理商')
            return
          }
        }
      }
      this.$router.push({
        name: 'refmulti',
        params: {
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname
        }
      })
    },
    clearRefValue (datamodel) {
      if (datamodel.value === '') {
        datamodel.valuehidden = ''
      }
    },
    onImageHead (file, detail) {
      // 上传
      let deviceFile = []
      let formData = new FormData()
      if (Array.isArray(file)) {
        deviceFile = file
      } else {
        deviceFile.push(file)
      }
      for (let i = 0; i < deviceFile.length; i++) {
        formData.append('image' + i, deviceFile[i].file, deviceFile[i].name)
      }
      this.axios({
        method: 'post',
        url: '/api/upload.imagedo',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.flag === '0') {
          let fileInfo = res.data.fileInfo
          for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
            let headData = this.jsondata.head[headIdx].groupvalue
            for (let i = 0; i < headData.length; i++) {
              if (headData[i].sField === detail.name) {
                headData[i].valuehidden = fileInfo[0].newname
                headData[i].value = fileInfo[0].oldname
              }
            }
          }
          Toast('上传成功')
        } else {
          Toast('上传失败')
        }
      }).catch(res => {
        //console.log(res)
      })
      // let headdata = this.jsondata.head[0].groupvalue
      //console.log(file)
      //console.log(detail)
    },
    delImageHead (file, detail) {
      for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
        let headData = this.jsondata.head[headIdx].groupvalue
        for (let i = 0; i < headData.length; i++) {
          if (headData[i].sField === detail.name) {
            let imgVal = headData[i].valuehidden.split(';')
            let imgTitle = headData[i].value.split(';')
            for (let j = 0; j < imgTitle.length; j++) {
              if (imgTitle[j] === file.file.name) {
                imgVal[j] = ''
                imgTitle[j] = ''
              }
            }
            let imgVal1 = ''
            let imgTitle1 = ''
            for (let j = 0; j < imgTitle.length; j++) {
              if (imgTitle[j] !== '') {
                imgVal1 += imgVal[j] + ';'
                imgTitle1 += imgTitle[j] + ';'
              }
            }
            headData[i].valuehidden = imgVal1
            headData[i].value = imgTitle1
          }
        }
      }
      Toast('删除成功')
    },
    onFileHead (file, detail) {
      // 上传
      let deviceFile = []
      let formData = new FormData()
      if (Array.isArray(file)) {
        deviceFile = file
      } else {
        deviceFile.push(file)
      }
      for (let i = 0; i < deviceFile.length; i++) {
        formData.append('file' + i, deviceFile[i].file, deviceFile[i].name)
      }
      this.axios({
        method: 'post',
        url: '/api/upload.filedo',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.flag === '0') {
          let fileInfo = res.data.fileInfo
          for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
            let headData = this.jsondata.head[headIdx].groupvalue
            for (let i = 0; i < headData.length; i++) {
              if (headData[i].sField === detail.name) {
                headData[i].valuehidden = fileInfo[0].newname
                headData[i].value = fileInfo[0].oldname
              }
            }
          }
          Toast('上传成功')
        } else {
          Toast('上传失败')
        }
      }).catch(res => {
        //console.log(res)
      })
      // let headdata = this.jsondata.head[0].groupvalue
      //console.log(file)
      //console.log(detail)
    },
    delFileHead (file, detail) {
      for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
        let headData = this.jsondata.head[headIdx].groupvalue
        for (let i = 0; i < headData.length; i++) {
          if (headData[i].sField === detail.name) {
            let fileVal = headData[i].valuehidden.split(';')
            let fileTitle = headData[i].value.split(';')
            for (let j = 0; j < fileTitle.length; j++) {
              if (fileTitle[j] === file.file.name) {
                fileVal[j] = ''
                fileTitle[j] = ''
              }
            }
            let fileVal1 = ''
            let fileTitle1 = ''
            for (let j = 0; j < fileTitle.length; j++) {
              if (fileTitle[j] !== '') {
                fileVal1 += fileVal[j] + ';'
                fileTitle1 += fileTitle[j] + ';'
              }
            }
            headData[i].valuehidden = fileVal1
            headData[i].value = fileTitle1
          }
        }
      }
      Toast('删除成功')
    }
  },
  activated () {
    //console.log(this.$store.state.ref_show)
    //console.log(this.$store.state.ref_id)
    if (this.$store.state.ref_show !== '') {
      this.clickmodel.value = this.$store.state.ref_show
      this.clickmodel.valuehidden = this.$store.state.ref_id
      this.clickmodel.errormsg = ''
      let fieldmap = this.clickmodel.fieldmap
      if (fieldmap !== undefined && fieldmap.length > 0) {
        let arrFieldmap = fieldmap.split(';')
        let item = this.$store.state.ref_item
        if (this.clickfrom === 'headdata') {
          for (let groupindex = 0; groupindex < this.jsondata.head.length; groupindex++) {
            let itemgroup = this.jsondata.head[groupindex]
            for (let itemindex = 0; itemindex < itemgroup.groupvalue.length; itemindex++) {
              let itemdata = itemgroup.groupvalue[itemindex]
              for (let fieldmapindex in arrFieldmap) {
                let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
                if (this.$store.state.ref_field === arrFieldMapKey[1]) {
                  continue
                }
                if (itemdata.sField === arrFieldMapKey[1]) {
                  if (itemdata.sUIType === 'ref') {
                    itemdata.value = item[arrFieldMapKey[0] + '_ref']
                    itemdata.valuehidden = item[arrFieldMapKey[0]]
                  } else if (itemdata.sUIType === 'enum') {
                    itemdata.value = item[arrFieldMapKey[0] + '_enum']
                    itemdata.valuehidden = item[arrFieldMapKey[0]]
                  } else {
                    itemdata.value = item[arrFieldMapKey[0]]
                  }
                }
              }
            }
          }
        } else if (this.clickfrom === 'bodydata') {
          for (let itemindex in this.jsondata.bodydata[this.clickindex]) {
            let itemdata = this.jsondata.bodydata[this.clickindex][itemindex]
            for (let fieldmapindex in arrFieldmap) {
              let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
              if (this.$store.state.ref_field === arrFieldMapKey[1]) {
                continue
              }
              if (itemdata.sField === arrFieldMapKey[1]) {
                if (itemdata.sUIType === 'ref') {
                  itemdata.value = item[arrFieldMapKey[0] + '_ref']
                  itemdata.valuehidden = item[arrFieldMapKey[0]]
                } else if (itemdata.sUIType === 'enum') {
                  itemdata.value = item[arrFieldMapKey[0] + '_enum']
                  itemdata.valuehidden = item[arrFieldMapKey[0]]
                } else {
                  itemdata.value = item[arrFieldMapKey[0]]
                }
              }
            }
          }
        }
      }
      this.clickmodel = {}
      this.clickindex = 0
      this.clickfrom = ''
      this.$store.commit('clearRef')
    }
  }
}
</script>

<style>
.fieldgroup_title {
  text-align: left;
  margin: 0px;
  padding: 25px 15px 15px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
}
</style>
